package com.example.androidstack;

import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText et;
    private Button popBtn,pushBtn;
    private TextView text;
    private List<String> list = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        initUI();
    }

    private void initUI(){
        et = (EditText)findViewById(R.id.main_EditText);
        pushBtn = (Button) findViewById(R.id.main_PushButton);
        pushBtn.setOnClickListener(this);
        popBtn = (Button)findViewById(R.id.main_PopButton);
        popBtn.setOnClickListener(this);
        text = (TextView)findViewById(R.id.main_text);
    }

    @Override
    public void onClick(View view) {
     if(view.getId()==R.id.main_PushButton){
         list.add(et.getText().toString());
         for (int index=list.size()-1;index>=0;index--){
             if(index==list.size()-1){
                 text.setText(list.get(list.size()-1)+"\n");
             }else {
                 text.append(list.get(index)+"\n");
             }
         }
     }
     if(view.getId()==R.id.main_PopButton){
        list.remove(list.size()-1);
         if(list.size()!=0) {
             for (int index = list.size() - 1; index >= 0; index--) {
                 if (index == list.size() - 1) {
                     text.setText(list.get(list.size() - 1) + "\n");
                 } else {
                     text.append(list.get(index) + "\n");
                 }

             }
         }else {
             text.setText("");
         }
     }
    }
}
